package com.ruoyi.mymod.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Calendar;
import java.util.Date;

public class Dateutil {

	public static Date lastDay(Date d) {
        Calendar ca= Calendar.getInstance();
		ca.setTime(d);
		ca.add(Calendar.DAY_OF_MONTH, -1);
		return ca.getTime();
	}


	//增加天数
	public static Date addd(Date d,int days) {
        Calendar ca= Calendar.getInstance();
		ca.setTime(d);
		ca.add(Calendar.DAY_OF_MONTH, days);
		return ca.getTime();
	}

	//增加月份
	public static Date addm(Date d,int months) {
        Calendar ca= Calendar.getInstance();
		ca.setTime(d);
		ca.add(Calendar.MONTH, months);
		return ca.getTime();
	}



	//去掉时分秒的时期
	public static Date getymd() {
		return getymd(new Date());
	}
	//去掉时分秒的时期
	public static Date getymd(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s = sdf.format(d);
		Date date=null;
		try {
			date = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	//去掉时分秒的时期
	public static Date getym() {
		return getymd(new Date());
	}
	//去掉时分秒的时期
	public static Date getym(Date d) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		String s = sdf.format(d);
		Date date=null;
		try {
			date = sdf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}





	public static String getymds(Date d) {
		return null;

	}

	public static String getDateString(Date date,String fmt) {
		SimpleDateFormat formatter = new SimpleDateFormat(fmt);
		return formatter.format(date);
	}

	public static final String yMd="yyyy-MM-dd";
	public static final String yMdHms="yyyy-MM-dd HH:mm:ss";
	public static final String yyyyMMdd="yyyyMMdd";
	public static final String yyyyMM="yyyyMM";


	public static String curDateymd() {	return getDateString(new Date(),yMd);}

	public static String curDateymdhms() {return getDateString(new Date(), yMdHms);}

	//@Test
	public void  getymds() {
//
//		LocalDate ld=LocalDate.now();
//		LocalTime lt=LocalTime.now();
//		LocalDateTime ldt=LocalDateTime.now();

		LocalDate date = LocalDate.of(1993, 12, 25);
		//十天以后
		//LocalDate dateAfter10Days = date.plusDays(-10);
		//LocalDate dateAfter10Days = date.plusMonths(-1);
		LocalDate dateAfter10Days = date.plusYears(-1);
		LocalDate dateBefore1Month = date.minusMonths(1);

		System.out.println(date.format(DateTimeFormatter.ofPattern("yyyyMMdd")));
		System.out.println(dateBefore1Month);

		System.out.println("十天以后:" + dateAfter10Days);
		//一个月前
		//LocalDate dateBefore1Month = date.minusMonths(1);
		System.out.println("一个月前:" + dateBefore1Month);
		//这个月的最后一天
		LocalDate lastDayOfMonth = date.with(TemporalAdjusters.lastDayOfMonth());
		System.out.println("这个月最后一天:" + lastDayOfMonth);
		//今年第一天
		System.out.println("今年第一天:" + date.with(TemporalAdjusters.firstDayOfYear()));


	}

}
